package com.p2p.base.util.mail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
//使用Sendcloud官方推荐的Httpclient库，发送post请求。Jodd，发送大数据量的邮件时，Sendcloud的Nginx返回414。
public class JavaWebapi {

	public static void send(String to, String subject, String html) {
		if (!check()) {
			return;
		}
		String url = URL;
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost(url);

		List nvps = new ArrayList();
		nvps.add(new BasicNameValuePair("api_user", API_USER));
		nvps.add(new BasicNameValuePair("api_key", API_KEY));
		nvps.add(new BasicNameValuePair("from", FROM));
		nvps.add(new BasicNameValuePair("to", to));
		nvps.add(new BasicNameValuePair("subject", subject));
		nvps.add(new BasicNameValuePair("html", html));
		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			HttpResponse response = httpclient.execute(httpost);

			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 正常返回
				HttpEntity entity = response.getEntity();
				System.out.println(EntityUtils.toString(entity));
			} else {
				System.err.println("error");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static String API_USER;
	public static String API_KEY;
	public static String URL;
	public static String FROM;

	private static Logger logger = Logger.getLogger(SendCloudMail.class);

	private static boolean check() {
		if (StringUtils.isEmpty(API_USER) || StringUtils.isEmpty(API_KEY)
				|| StringUtils.isEmpty(FROM) || StringUtils.isEmpty(URL)) {
			logger.error("sendcloud mail is not configured when send mail!");
			return false;
		}
		return true;

	}

	public static void main(String[] args) throws ClientProtocolException,
			IOException {
		testInit();
		String to = "240370818@qq.com";
		String subject = "投标协议";
		String html = "你太棒了！你已成功的从SendCloud发送了一封测试邮件，接下来快登录前台去完善账户信息吧！";
		send(to, subject, html);

		// String subject2 = "来自SendCloud的第一封邮件！";
		// String html2 = "发送失败";
		// send(to, subject2, html2);
	}

	public static void testInit() {
		API_USER = "whptp_test_J6wu6k";
		API_KEY = "DEZQHP9GhfSOxXju";
		URL = "https://sendcloud.sohu.com/webapi/mail.send.json";
		FROM = "master@whptp.com";
	}

	public static void init(Map<String, String> ctxPropertiesMap) {
		String apiUser = MapUtils.getString(ctxPropertiesMap, "mail.api_user");
		String apiKey = MapUtils.getString(ctxPropertiesMap, "mail.api_key");
		String url = MapUtils.getString(ctxPropertiesMap, "mail.url");
		String from = MapUtils.getString(ctxPropertiesMap, "mail.from");
		if (StringUtils.isEmpty(apiUser)) {
			String errorTip = "sendCound error:";
			logger.error(errorTip + "apiUser is empty");
		} else {
			API_USER = apiUser;
		}
		if (StringUtils.isEmpty(apiKey)) {
			String errorTip = "sendCound error:";
			logger.error(errorTip + "apiKey is empty");
		} else {
			API_KEY = apiKey;
		}
		if (StringUtils.isEmpty(url)) {
			String errorTip = "sendCound error:";
			logger.error(errorTip + "url is empty");
		} else {
			URL = url;
		}
		if (StringUtils.isEmpty(from)) {
			String errorTip = "sendCound error:";
			logger.error(errorTip + "from is empty");
		} else {
			FROM = from;
		}

	}

}